package com.tmc.web.controller.intair;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.constant.FareConstant.PolicyStatus;
import com.tmc.entity.intair.TripartitePolicy;
import com.tmc.service.intair.TripartitePolicyServiceImpl;
import com.tmc.web.BaseController;



@Controller
@RequestMapping(value="/tripartitepolicy")
public class TripartitePolicyController extends BaseController {
	
	
	@Autowired
	private TripartitePolicyServiceImpl policyService; 
	
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public ModelAndView lsit(TripartitePolicy cond, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<TripartitePolicy> page = policyService.findByCond(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/policy/tripartitepolicy-list", map);		
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long policyId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		TripartitePolicy policy = null;
		if (policyId != null){
			policy = policyService.getById4Show(context, policyId);
		}else{
			policy = new TripartitePolicy();
		}			
		map.put("policy", policy);
		map.put("context", context);
		return new ModelAndView("views/policy/tripartitepolicy-edit", map);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(TripartitePolicy policy, BindingResult result, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if (result.hasErrors()) {
			return JSON.toJSONString(CommonResult.makeFail(result.getFieldError().getDefaultMessage()));
		}
		CommonResult cr = policyService.savePolicy(context, policy);
		return cr.toJson();
	}
	
	/**
	 * 删除政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/delete{policyId}")
	@ResponseBody
	public String policyDelete(Long policyId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.deletePolicy(context, policyId);
		return cr.toJson();
	}

	/**
	 * 删除政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/deleteAll{policyIds}")
	@ResponseBody
	public String policysDelete(String policyIds, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.deletePolicys(context, policyIds);
		return cr.toJson();
	}

	/**
	 * 上架政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/puton{policyId}")
	@ResponseBody
	public String policyPutOn(Long policyId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.changePolicyStatus(context, policyId, PolicyStatus.UP.getStatus());
		if (cr.isSuccess()) {
			cr.setMessage("已发布！");
		}
		return cr.toJson();
	}

	/**
	 * 上架政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/putons{policyIds}")
	@ResponseBody
	public String policysPutOn(String policyIds, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.changePolicysStatus(context, policyIds, PolicyStatus.UP.getStatus());
		if (cr.isSuccess()) {
			cr.setMessage("已发布！");
		}
		return cr.toJson();
	}

	/**
	 * 下架政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/pulloff{policyId}")
	@ResponseBody
	public String policyPullOff(Long policyId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.changePolicyStatus(context, policyId, PolicyStatus.DOWN.getStatus());
		if (cr.isSuccess()) {
			cr.setMessage("已下架！");
		}
		return cr.toJson();
	}

	/**
	 * 下架政策
	 * 
	 * @param policy
	 * @return
	 */
	@RequestMapping(value = "/pulloffs{policyIds}")
	@ResponseBody
	public String policysPullOff(String policyIds, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = policyService.changePolicysStatus(context, policyIds,
				PolicyStatus.DOWN.getStatus());
		if (cr.isSuccess()) {
			cr.setMessage("已下架！");
		}
		return cr.toJson();
	}
	
	/**
	 * 复制政策
	 * 
	 * @param policyId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/copy")
	public ModelAndView policyCopy(Long policyId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		TripartitePolicy policy = null;
		if(policyId != null){
			policy = policyService.copyPolicy(context,policyId);
		}else{
			policy = new TripartitePolicy();
		}
		map.put("policy", policy);
		return new ModelAndView("views/policy/tripartitepolicy-edit", map);
	}
	
	
}
